package com.second.app.thread.haswaiters;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author gyz
 * @date 2021/2/2 15:37
 */
public class Service {
    public final ReentrantLock lock = new ReentrantLock();

    public Condition condition = lock.newCondition();

    public void waitMethod() {
        lock.lock();
        try {
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void wakeUp() {
        lock.lock();
        try {
            System.out.println("有没有线程正在等待此condition：" + lock.hasWaiters(condition));
            System.out.println("等待此condition的线程个数：" + lock.getWaitQueueLength(condition));
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

}
